<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/1/15
 * Time: 12:34
 */

namespace app\common\service;


class CacheRunning
{
    const RUNNING_KEY = 'CacheRunning:bg/automail/run';
    public $running_key ;
    /**
     * @var \yii\caching\CacheInterface
     */
    public $cache ;

    /**
     * @return string
     */
    public function getRunningKey()
    {
        return $this->running_key;
    }

    public function __construct($key = '')
    {
        if(!empty($key)){
            $this->running_key = $key;
        }else{
            $this->running_key = self::RUNNING_KEY;
        }

        $this->cache = \Yii::$app->cache;
    }

    public function get(){
        $data = $this->cache->get($this->getRunningKey());
        return $data;
    }
    public function isRunning(){
        return !empty($this->get());
    }
    public function running(){
        $this->cache->set($this->getRunningKey(),1);
    }

    public function stop(){
        $this->cache->set($this->getRunningKey(),0);
    }
}